package com.chunxiadong.controller.admin;

import com.chunxiadong.entity.FriendLink;
import com.chunxiadong.service.FriendLinkService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @auther QiuShangcheng
 * @create 2021/6/8
 */
@Controller
@RequestMapping("/admin/friendLink")
public class FriendLinkController {

    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    FriendLinkService friendLinkService;

    /**
     * 删除友情链接
     * @param id
     * @param attributes
     * @return
     */
    @RequestMapping("/deleteFriendLink/{id}")
    public String deleteFriendLink(@PathVariable Integer id,RedirectAttributes attributes){
        int i = friendLinkService.deleteFriendLinkById(id);
        attributes.addFlashAttribute("friendLink_message", "Delete_friendLink Successful!");
        return "redirect:/admin/friendLink";
    }

    //    跳转友链新增页面
    @RequestMapping("/input")
    public String inputFriendLink(Model model) {
        model.addAttribute("friendLink", new FriendLink());
        return "admin/friendLink-input";
    }

    //  跳转到友情链接修改页面
    @RequestMapping("/{id}/input")
    public String inputUpdateFriendLink(@PathVariable Integer id, Model model) {
        model.addAttribute("friendLink", friendLinkService.queryFriendLinkById(id));
        return "admin/friendLink-input";
    }

    /**
     * 添加友情链接
     *
     * @param friendLink
     * @return
     */
    @RequestMapping("/addFriendLink")
    public String addFriendLink(FriendLink friendLink, RedirectAttributes model) {
        FriendLink queryRes = friendLinkService.queryFriendLinkByBlogAddr(friendLink.getBlogAddr());
        if (queryRes != null) {
            model.addFlashAttribute("friendLink_message", "Do not add friendLink repeatedly!");
            log.info(friendLink.toString() + "\nResult : {" + " Add_FriendLink repeatedly!" + "}");
            return "redirect:/admin/friendLink/input";
        }
        int i = friendLinkService.addFriendLink(friendLink);
        if (i == 0) {
            model.addFlashAttribute("friendLink_message", " Add_FriendLink Case Error!");
            log.info(friendLink.toString() + "\nResult : {" + " Add_FriendLink Case Error!" + "}");
        }
        model.addFlashAttribute("friendLink_message", "Add_FriendLink Successful!");
        return "redirect:/admin/friendLink";
    }

    /**
     * 修改友情链接信息
     * @param id
     * @param friendLink
     * @param model
     * @return
     */
    @RequestMapping("/updateFriendLink/{id}")
    public String updateFriendLink(@PathVariable Integer id, FriendLink friendLink, RedirectAttributes model){
        FriendLink queryRes = friendLinkService.queryFriendLinkById(id);
        if (queryRes == null){
            model.addFlashAttribute("friendLink_message", "No friendLink_Data!");
            log.info(friendLink.toString() + "\nResult : {" + " No friendLink_Data!" + "}");
            return "redirect:/admin/friendLink";
        }
        int i = friendLinkService.updateFriendLink(friendLink);
        if (i == 0){
            model.addFlashAttribute("friendLink_message", " Update_FriendLink Case Error!");
            log.info(friendLink.toString() + "\nResult : {" + " Update_FriendLink Case Error!" + "}");
        }
        model.addFlashAttribute("friendLink_message","Update_FriendLink Successful!");
        return "redirect:/admin/friendLink";
    }
}
